﻿using System;
using System.Collections.Generic;
using System.Linq;

public class Student
{
    public string StudentId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string Major { get; set; }

    public Student(string studentId, string name, int age, string major)
    {
        StudentId = studentId;
        Name = name;
        Age = age;
        Major = major;
    }

    public override string ToString()
    {
        return $"学号: {StudentId}, 姓名: {Name}, 年龄: {Age}, 专业: {Major}";
    }
}

public class StudentManager
{
    private List<Student> students = new List<Student>();

    // 1. 增加学生
    public void AddStudent(Student student)
    {
        students.Add(student);
        Console.WriteLine($"成功添加学生: {student}");
    }

    // 2. 修改学生
    public bool UpdateStudent(string studentId, string name, int age, string major)
    {
        var student = students.FirstOrDefault(s => s.StudentId == studentId);
        if (student != null)
        {
            student.Name = name;
            student.Age = age;
            student.Major = major;
            Console.WriteLine($"成功修改学生: {student}");
            return true;
        }
        Console.WriteLine($"未找到学号为 {studentId} 的学生");
        return false;
    }

    // 3. 删除学生
    public bool DeleteStudent(string studentId)
    {
        var student = students.FirstOrDefault(s => s.StudentId == studentId);
        if (student != null)
        {
            students.Remove(student);
            Console.WriteLine($"成功删除学生: {student}");
            return true;
        }
        Console.WriteLine($"未找到学号为 {studentId} 的学生");
        return false;
    }

    // 4. 查询学生
    public Student FindStudent(string studentId)
    {
        return students.FirstOrDefault(s => s.StudentId == studentId);
    }

    // 5. 遍历所有学生
    public void DisplayAllStudents()
    {
        Console.WriteLine("\n=== 所有学生信息 ===");
        if (students.Count == 0)
        {
            Console.WriteLine("没有学生信息");
            return;
        }

        foreach (var student in students)
        {
            Console.WriteLine(student);
        }
    }
}

// 测试类
class Program
{
    static void Main(string[] args)
    {
        StudentManager manager = new StudentManager();

        // 测试增加学生
        manager.AddStudent(new Student("2024001", "张三", 20, "计算机科学"));
        manager.AddStudent(new Student("2024002", "李四", 19, "软件工程"));
        manager.AddStudent(new Student("2024003", "王五", 21, "网络安全"));

        // 测试遍历
        manager.DisplayAllStudents();

        // 测试查询
        Console.WriteLine("\n=== 查询学生 ===");
        var student = manager.FindStudent("2024002");
        Console.WriteLine(student != null ? $"找到学生: {student}" : "未找到学生");

        // 测试修改
        Console.WriteLine("\n=== 修改学生 ===");
        manager.UpdateStudent("2024002", "李四四", 20, "人工智能");

        // 测试删除
        Console.WriteLine("\n=== 删除学生 ===");
        manager.DeleteStudent("2024003");

        // 最终显示
        manager.DisplayAllStudents();
    }
}